import math
import pyperclip
import profile
import helpers.FileHelper as FileHelper
import helpers.MiscHelper as MiscHelper
import helpers.MathHelper as MathHelper
import helpers.GUIHelper as GUIHelper
import package.PackageManager as PackageManager


def cmd_smart_export_douyin(opts, args):
    opts["check-ascii"] = None
    opts, args = MiscHelper.merge_args(profile.effect_export_args, [], opts, args)
    src = profile.effect_workspace
    dst = GUIHelper.ask_save()
    pkg = PackageManager.parse(src)
    pkg.setup(opts)
    FileHelper.cp(dst if dst.endswith(".zip") else dst+".zip", (profile.effect_temporary, "export.zip"))
    FileHelper.zip(src, dst)


def cmd_smart_export_without_binary(opts, args):
    opts["binary"] = None
    opts, args = MiscHelper.merge_args(profile.effect_export_args, [], opts, args)
    src = profile.effect_workspace
    dst = GUIHelper.ask_save()
    pkg = PackageManager.parse(src)
    pkg.setup(opts)
    FileHelper.cp(dst if dst.endswith(".zip") else dst + ".zip", (profile.effect_temporary, "export.zip"))
    FileHelper.zip(src, dst)


def cmd_shape_blur_max_radius(opts, args):
    def f(x, y):
        return pow(abs(x), 2.0) * 0.5 + 1.5 + 0.8 * MathHelper.sign(y) * pow(1.0 - abs(x), 2.0)

    m = 0
    for i in range(3600):
        r = math.radians(i * 0.1)
        m = max(f(math.cos(r), math.sin(r)), m)
    pyperclip.copy(str(m))


def cmd_smart_export_nohash(opts, args):
    opts["rehash"] = None
    opts, args = MiscHelper.merge_args(profile.effect_export_args, [], opts, args)
    src = profile.effect_workspace
    dst = GUIHelper.ask_save()
    pkg = PackageManager.parse(src)
    pkg.setup(opts)
    FileHelper.cp(dst if dst.endswith(".zip") else dst+".zip", (profile.effect_temporary, "export.zip"))
    FileHelper.zip(src, dst)